package day_2022_7_to_8.day48;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author haomin
 * @date 2022/08/12 23:32
 **/
public class Day27_1_Num1282_GroupThePeople {
    class Solution {
        public List<List<Integer>> groupThePeople(int[] groupSizes) {
            List<List<Integer>> res = new ArrayList<>();
            PriorityQueue<int[]> qu = new PriorityQueue<>((a, b)->{return a[0]-b[0];});//小根堆
            for(int i=0;i<groupSizes.length;i++){
                qu.offer(new int[]{groupSizes[i],i});
            }
            while(!qu.isEmpty()){
                int[] heap = qu.poll();
                int nextNum = heap[0]-1;//加上它自己需要额外几个人来组
                List<Integer> list = new ArrayList<>();
                list.add(heap[1]);
                for(int i=0;i<nextNum;i++){
                    int[] next = qu.poll();
                    list.add(next[1]);
                }
                res.add(list);
            }
            return res;
        }
    }
}